package com.tenebrous.jestic.service.framwork.system;

import com.alibaba.fastjson.JSONObject;
import com.tenebrous.jestic.entity.framwork.system.SysParams;
import com.tenebrous.jestic.mapper.idao.framwork.system.SysParamsMapper;
import com.tenebrous.jestic.service.SuperService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class SysParamsService extends SuperService<SysParams> {

    @Autowired
    private SysParamsMapper mapper;

    @Override
    @Autowired
    protected void setSuperMapper() {
        super.setSuperMapper(this.mapper);
    }

    @Transactional
    public ApiResult updateParams(String fitem) {

        JSONObject params = JSONObject.parseObject(fitem);
        String userid = params.getString("userid");
        String realname = params.getString("realname");
        List<Map<String,Object>> tips = (List<Map<String, Object>>) params.get("tips");

        if(tips != null){
            for(Map<String,Object> map : tips){

                SysParams userParam = this.mapper.selectParamsByUseridAndSkey(userid,map.get("skey").toString());
                if(userParam == null){
                    //新增
                    SysParams param = new SysParams();
                    param.setId(UUIDUtils.randomUUID());
                    param.setUserid(userid);
                    param.setSkey(map.get("skey").toString());
                    param.setSname(map.get("skey").toString());
                    param.setSvalue(map.get("svalue")==null? "":map.get("svalue").toString());
                    param.setCreateDate(new Date());
                    param.setCreateUserid(userid);
                    param.setCreateUsername(realname);
                    this.mapper.insertSelective(param);
                }else{
                    //更新
                    userParam.setSvalue(map.get("svalue")==null? "":map.get("svalue").toString());
                    this.mapper.updateByPrimaryKeySelective(userParam);
                }
            }
        }else{
            return new ApiResult<>(999, "保存失败。");
        }
        return new ApiResult<>(200, "保存成功。");
    }

    public List<Map<String, Object>> getParams(String userid, String[] skeys) {

        List<Map<String,Object>> list = new ArrayList<>();
        if(skeys != null){
            for (String skey: skeys){
                SysParams userParam = this.mapper.selectParamsByUseridAndSkey(userid,skey);
                if(userParam != null){
                    Map<String,Object> map = new HashMap<>();
                    map.put("skey",skey);
                    map.put("svalue",userParam.getSvalue());
                    list.add(map);
                }
            }
        }
        return list;
    }

    public List<SysParams> queryParamsBySkey(String skey) {
        return this.queryParamsBySkey(skey, null);
    }

    public List<SysParams> queryParamsBySkey(String skey, String userid) {
        return this.mapper.selectByCondition(skey, userid);
    }
}
